package com.yzmoney.common.service.impl;

import com.yzmoney.common.constant.PropertyUtils;
import com.yzmoney.common.service.SaveUserService;
import com.yzmoney.common.util.HttpClientUtil;
import com.yzmoney.common.util.JDKStackTrace;
import com.yzmoney.common.util.JsonUtils;
import com.yzmoney.wechat.model.token.AuthUserInfoJson;
import com.yzmoney.wechat.service.WechatOAuthService;
import com.yzmoney.wechat.service.WechatUserService;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by DOVE on 2015/10/14.
 */
@Service
public class SaveUserServiceImpl implements SaveUserService {
    private static Logger logger = Logger.getLogger(SaveUserServiceImpl.class);

    //微信2.0 用户信息接口
    @Autowired
    private WechatOAuthService wechatOAuthService;

    //微信用户信息接口
    @Autowired
    private WechatUserService wechatUserService;

    private String updateUserCancelUrl = PropertyUtils.getValue("update_user_cancle_url", "");

    private String updateUserAddUrl = PropertyUtils.getValue("update_user_add_url", "");

    public boolean saveUserInfo(AuthUserInfoJson userInfoJson) {
        return false;
    }

    @Override
    public boolean saveUserInfo(final String openId) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                sendHttp(updateUserAddUrl, openId);
            }
        }).start();
        return true;
    }

    @Override
    public boolean cancelSubscriber(String openId) {
        return sendHttp(updateUserCancelUrl, openId);
    }

    private boolean sendHttp(String url, String openId) {
        AuthUserInfoJson userInfoJson = wechatUserService.loadWechatUserInfo(openId);
        if (userInfoJson != null && StringUtils.isNotEmpty(url)) {
            Map<String, String> param = new HashMap<String, String>();
            param.put("userJson", JsonUtils.toJson(userInfoJson));
            try {
                logger.info("openId:" + openId + ",userinfoJson:" + JsonUtils.toJson(userInfoJson) + "sendHttp--url：" + url);
                String result = HttpClientUtil.get(url, param, null);
                if (result != null) {
                    JSONObject object = JSONObject.fromObject(result);
                    if (object != null && object.getInt("code") == 0) {
                        logger.info("更新微信用户信息成功 openId:" + openId + ",userinfoJson:" + JsonUtils.toJson(userInfoJson) + "sendHttp--url：" + url + ",result：" + result);
                        return true;
                    } else {
                        logger.info("更新微信用户信息失败 openId:" + openId + ",userinfoJson:" + JsonUtils.toJson(userInfoJson) + "sendHttp--url：" + url + ",result：" + result);
                    }
                }
            } catch (Exception e) {
                logger.info("openId:" + openId + ",userinfoJson:" + JsonUtils.toJson(userInfoJson) + "sendHttp--url：" + url + JDKStackTrace.getJDKStrack(e));
                return false;
            }
        }
        return false;
    }
}
